package com.hanrui.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanrui.android.R;
import com.hanrui.android.adapter.CheckBoxAdapter;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.BossListBean;
import com.hanrui.android.model.CheckBean;
import com.hanrui.android.model.GetNextActvityBean;
import com.hanrui.android.utils.BaseUtils;
import com.hanrui.android.library.widget.TitleBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 全选反选
 * Created by zw on 2018/3/14.
 */
public class TermActivity extends ActionBarBaseActivity {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.login_out)
    TextView loginOut;
    @BindView(R.id.login_rl)
    RelativeLayout loginRl;
    int key;
    boolean isSingle;
    List<CheckBean> dataList = new ArrayList<>();
    CheckBoxAdapter adapter;
    boolean checked ;


    Bundle bundle;
    ArrayList<GetNextActvityBean.NextActivityBean.NextAssigneeListBean> datas;
    ArrayList<BossListBean.ListDateBean> bossList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_term);

        unbinder = ButterKnife.bind(this);

        mTitleBar.setTitle("处理人");

        TextView textView = (TextView) mTitleBar.addAction(new TitleBar.TextAction("确定") {
            @Override
            public void performAction(View view) {

                Map<String,String> map = BaseUtils.getData(dataList);
                Intent intent = new Intent();
                intent.putExtra("data", map.get("data"));
                intent.putExtra("id", map.get("id"));
                intent.putExtra("count",map.get("count"));
                Log.e(Constant.tag,key+"");
                setResult(key,intent);
                TermActivity.this.finish();

            }
        });

        textView.setTextColor(ContextCompat.getColor(this,R.color.white));

        init();
    }


    @Override
    protected void initInjector() {
        //mActivityComponent.inject(this);
    }



    private void init() {

        key = getIntent().getIntExtra("key",0);
        isSingle = getIntent().getBooleanExtra("isSingle",false);
        Log.i(Constant.tag,key+"");
        if (key == Constant.nextactivity){

            Log.i(Constant.tag,key+"");
            bundle = getIntent().getExtras();
            if (null != bundle.getSerializable("next")){
                datas = (ArrayList<GetNextActvityBean.NextActivityBean.NextAssigneeListBean>) bundle.getSerializable("next");
                for (GetNextActvityBean.NextActivityBean.NextAssigneeListBean bean:datas){
                    dataList.add(new CheckBean(bean.getName(),false,bean.getId()));
                }
                adapter = new CheckBoxAdapter(dataList,this,isSingle);
                listview.setAdapter(adapter);

            }
        }else if (key == Constant.leader) {

            bundle = getIntent().getExtras();
            if (null != bundle.getSerializable("next")){
                bossList = (ArrayList<BossListBean.ListDateBean>) bundle.getSerializable("next");
                for (BossListBean.ListDateBean bean:bossList){
                    dataList.add(new CheckBean(bean.getName(),false,bean.getId()));
                }
                adapter = new CheckBoxAdapter(dataList,this,isSingle);
                listview.setAdapter(adapter);

            }

        		}

        if (isSingle) {

            loginRl.setVisibility(View.GONE);

        		}else {

            loginRl.setVisibility(View.VISIBLE);
        }


    }

    @OnClick({ R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_out:
                for(int i=0;i<dataList.size();i++){
                    if(!checked){
                        dataList.get(i).setIscheck(true);
                    }else {
                        dataList.get(i).setIscheck(false);
                    }
                }
                adapter.notifyDataSetChanged();
                if(!checked){
                    checked = true;
                    loginOut.setText("取消");
                }else {
                    checked = false;
                    loginOut.setText("全选");
                }
                break;
        }
    }


    public static void start(Activity activity, int key, List list,boolean isSingle){

        Intent intent = new Intent(activity,TermActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("key",key);
        bundle.putBoolean("isSingle",isSingle);
        bundle.putSerializable("next", (Serializable) list);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent,1000);

    }

    public static void start(Fragment fragment, int key, List list,boolean isSingle){

        Intent intent = new Intent(fragment.getActivity(),TermActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("key",key);
        bundle.putBoolean("isSingle",isSingle);
        bundle.putSerializable("next", (Serializable) list);
        intent.putExtras(bundle);
        fragment.startActivityForResult(intent,1000);

    }
}
